﻿using AutoMapper;
using CommonType;
using MediatR;
using S_Incident.API.Application.Commands;
using S_Incident.Domains.WorkflowEngine;
using S_Incident.Infrasturctre.Interface;

namespace S_Incident.API.Application.CommandsHanders
{
    public class NodeSqlSuageCreateCommandHanders : IRequestHandler<NodeSqlSuageCreateCommand, ApiResule<int>>
    {
        private readonly INodeRepository nodeRepository;
        private readonly IMapper mapper;
        public NodeSqlSuageCreateCommandHanders(INodeRepository nodeRepository, IMapper mapper)
        {
            this.nodeRepository = nodeRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 新增节点信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResule<int>> Handle(NodeSqlSuageCreateCommand request, CancellationToken cancellationToken)
        {
            // 获取节点数量
            //int nodeCount = request.NodeDtos!.ProcessingTime!.Split(';').Length;

            //var nodes = new List<Node>(nodeCount);

            var nodes = new List<Node>(8);
            var nodeTimes = request.NodeDtos!.ProcessingTime!.Split(';');
            var numberDays = request.NodeDtos!.NumberDays!.Split(";");
            var nodeNames = request.NodeDtos!.NodeName!.Split(';');
            var nodeOpertors = request.NodeDtos.NodeOperator!.Split(";");
            var nodeTypes = request.NodeDtos!.NodeType!.Split(';');
            for (int i = 0; i < 8; i++)
            {

                var node = new Node
                {
                    ProcessingTime = nodeTimes[i].Split(':')[1],
                    NumberDays = numberDays[i].Split(':')[1],
                    NodeName = nodeNames[i],
                    NodeOperator = nodeOpertors[i].Split(':')[1],
                    NodeType = nodeTypes[i],
                };
                nodes.Add(node);
            }
            var result = await nodeRepository.AddAsync(nodes);
            if (result > 0)
            {
                return new ApiResule<int>
                {
                    Code = 200,
                    Msg = "新增成功",
                    Result = result
                };
            }
            else
            {
                return new ApiResule<int>
                {
                    Code = 250,
                    Msg = "新增失败",
                    Result = result
                };
            }
        }
    }
}
